﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using DevExpress.Xpo;
using ChuanPlus;
using MySql.Data;
using System.Data;
using MySql.Data.MySqlClient;

namespace ChuanPlus.DAO
{
    class StockCardDAO
    {
        //
        // get stock card transaction base on fromDate and toDate
        //
        public static DataTable GetStockCard(Product product, DateTime fromDate, DateTime toDate, Warehouse warehouse)
        {
            try
            {
                MySqlParameter[] param = new MySqlParameter[4];
                param[0] = new MySqlParameter("?fromDate", MySqlDbType.DateTime);
                param[0].Value = fromDate;

                param[1] = new MySqlParameter("?toDate", MySqlDbType.DateTime);
                param[1].Value = toDate;

                param[2] = new MySqlParameter("?idProduct", MySqlDbType.Int32);
                param[2].Value = product.ID;

                param[3] = new MySqlParameter("?idwarehouse", MySqlDbType.Int32);
                param[3].Value = warehouse.warehouseId;

                return DataProvider.execute("getStockCard", param);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
                
            }
        }
        //
        // weekly stock card transaction base on fromDate and toDate
        //
        public static DataTable GetWeeklyStockCard(Warehouse warehouse, DateTime fromDate, DateTime toDate)
        {
            try
            {
                MySqlParameter[] param = new MySqlParameter[3];
                param[0] = new MySqlParameter("?fromDate", MySqlDbType.DateTime);
                param[0].Value = fromDate;

                param[1] = new MySqlParameter("?toDate", MySqlDbType.DateTime);
                param[1].Value = toDate;

                param[2] = new MySqlParameter("?idwarehouse", MySqlDbType.Int32);
                param[2].Value = warehouse.warehouseId;

                return DataProvider.execute("getWeeklyStockCard", param);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
                
            }
        }
        public static StockCard getStockCardById(int tranId)
        {
            XPQuery<StockCard> stockCard = new XPQuery<StockCard>(Session.DefaultSession);
            var list = (from st in stockCard
                        where st.TransID == tranId
                        select st).First();
            return list;
        }
        public static List<StockCard> getStockCardByOrderId(string orderId)
        {
            XPQuery<StockCard> stockCard = new XPQuery<StockCard>(Session.DefaultSession);
            var list = from st in stockCard
                       where st.TrackNo.Contains(orderId)
                       select st;
            List<StockCard> listStockCard = new List<StockCard>(list.AsEnumerable());
            return listStockCard;
        }
        public static long GenerateId()
        {
            XPQuery<StockCard> stockCard = new XPQuery<StockCard>(Session.DefaultSession);
            var list = (from st in stockCard
                        select st).Max(p => p.TransID);
            return list;
        }
    }
}
